//
//  welcomeViewController.m
//  0908-cckv
//
//  Created by zhoubaitong on 2017/9/11.
//  Copyright © 2017年 cckv. All rights reserved.
//

#import "welcomeViewController.h"
#import "ViewController.h"
#import "recordViewController.h"

#import <Foundation/Foundation.h>

@interface welcomeViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btn1H;
@property (weak, nonatomic) IBOutlet UIButton *leftCar;
@property (weak, nonatomic) IBOutlet UIButton *rightCar;
@property (weak, nonatomic) IBOutlet UIImageView *leftCarImageV;
@property (weak, nonatomic) IBOutlet UIImageView *rightCarImageV;
@property (weak, nonatomic) UIImageView *selectImageV;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btn1top;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btn2Top;

@property (weak, nonatomic) IBOutlet UIButton *recordBtn;
@property (weak, nonatomic) IBOutlet UIButton *playBtn;
@property (weak, nonatomic) IBOutlet UIButton *setBtn;
@property (strong, nonatomic) ZJAnimationPopView *popView;
@property (strong, nonatomic) tipView *tipV;
@end

@implementation welcomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    if (kScreenWidth == 320) {
        self.btn1top.constant = 30;
        self.btn1H.constant = 45;
    }
    
    self.selectImageV = self.leftCarImageV;
    [self.leftCar setImage:[[UIImage imageNamed:@"箭头.png"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateSelected];
    [self.rightCar setImage:[[UIImage imageNamed:@"箭头.png"] imageByTintColor:[UIColor whiteColor]] forState:UIControlStateSelected];
    self.leftCar.transform = CGAffineTransformRotate(self.leftCar.transform, M_PI);//旋转180
    self.rightCar.transform = CGAffineTransformRotate(self.rightCar.transform, M_PI);//旋转180
    
    [self.recordBtn setImage:[[UIImage imageNamed:@"奖杯.png"] imageByTintColor:[UIColor colorWithRed:(254/255.0) green:(242/255.0) blue:(58/255.0) alpha:1]] forState:UIControlStateNormal];
    
    [self.playBtn setTitle:NSLocalizedString(@"play game",nil) forState:UIControlStateNormal];
    [self.setBtn setTitle:NSLocalizedString(@"setting",nil) forState:UIControlStateNormal];

    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.leftCarImageV.hidden = YES;
    self.rightCarImageV.hidden = YES;
    self.leftCar.hidden = YES;
    self.rightCar.hidden = YES;
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"playMusic"] boolValue]) {
        
        [[KVPlayer shareTool] palyWithName:@"game_music" andType:@".mp3"];
    }
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    self.leftCarImageV.x = -kScreenWidth/2;
    self.rightCarImageV.x = kScreenWidth;
    
    self.leftCarImageV.hidden = NO;
    self.rightCarImageV.hidden = NO;
    
    [self setAnimation];

}

- (void)setAnimation
{
    [UIView animateWithDuration:0.5 animations:^{
        self.leftCarImageV.x = 0;
        self.rightCarImageV.x = 93;
    } completion:^(BOOL finished) {
        self.leftCar.hidden = NO;
        self.rightCar.hidden = NO;
        [self selectLeftCar:nil];
    }];
}

- (IBAction)selectLeftCar:(UIButton *)sender {
//    self.leftCar.selected = !self.leftCar.selected;
//    self.rightCar.selected = !self.leftCar.selected;
    if (!self.leftCar.selected) {
        self.leftCar.selected = YES;
        self.rightCar.selected = NO;
    }
    
    self.selectImageV = self.leftCarImageV;
    
    [[KVPlayer shareTool]playAudio:@"wang571" withType:@"wav"];

}
- (IBAction)selectRightCar:(UIButton *)sender {

    if (!self.rightCar.selected) {
        self.rightCar.selected = YES;
        self.leftCar.selected = NO;
    }
    self.selectImageV = self.rightCarImageV;
    [[KVPlayer shareTool]playAudio:@"wang571" withType:@"wav"];

}

- (IBAction)begin:(id)sender {
    
    [self heartButtonClick];
    
    
}
- (IBAction)set:(id)sender {
    
    tipView *v= [tipView vieFromXib];
    v.center = self.view.center;
    v.size = CGSizeMake(kScreenWidth-30, 214);
    self.tipV = v;
    v.top.text = NSLocalizedString(@"设置音效", nil);
    v.tip.text = NSLocalizedString(@"是否开启背景音乐", nil);
    [v.btn setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [v.btn2 setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateNormal];
    [v.btn addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
    [v.btn2 addTarget:self action:@selector(queding) forControlEvents:UIControlEventTouchUpInside];
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"playMusic"] boolValue]) {
        v.switchBtn.on = YES;
    }else{
        v.switchBtn.on = NO;
    }

    ZJAnimationPopView *popView = [[ZJAnimationPopView alloc]initWithCustomView:v popStyle:ZJAnimationPopStyleShakeFromTop dismissStyle:ZJAnimationDismissStyleDropToBottom];
    [popView pop];
    self.popView = popView;
    
}
- (void)quxiao{
    [self.popView dismiss];
}
- (void)queding
{
    
    if (self.tipV.switchBtn.on) {
        if ([[KVPlayer shareTool]isPlay]) {
            
        }else{
            [[KVPlayer shareTool] palyWithName:@"game_music" andType:@".mp3"];
            
        }
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"playMusic"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"playMusic"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        if ([[KVPlayer shareTool]isPlay]) {
            [[KVPlayer shareTool] stop];
            
        }else{
            
        }
    }
    [self.popView dismiss];
}
- (void)ok:(UIButton*)btn{

    NSString *s = [[NSUserDefaults standardUserDefaults]objectForKey:@"play"];
    if (s) {
        if ([s isEqualToString:@"YES"]) {
            
        }else{
            [[KVPlayer shareTool]paly];
        }
    }
    [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"play"];
    
    
}
- (void)no:(UIButton*)btn{

    NSString *s = [[NSUserDefaults standardUserDefaults]objectForKey:@"play"];
    if (s) {
        if ([s isEqualToString:@"YES"]) {
            [[KVPlayer shareTool]stop];
        }else{
            
        }
    }
    [[NSUserDefaults standardUserDefaults]setObject:@"NO" forKey:@"play"];
    
    
}

- (void)heartButtonClick{

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationRepeatCount:1];
    [UIView setAnimationDuration:0.25];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    
    CAKeyframeAnimation *k = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
    k.values =  @[@(1.0),@(1.5),@(1.0)];
    //    k.keyTimes = @[@(0.0),@(0.5),@(0.8),@(1.0)];
    k.calculationMode = kCAAnimationLinear;
    [self.selectImageV.layer addAnimation:k forKey:@"SHOW"];
    
    
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(go)];
    [UIView commitAnimations];

}

- (void)go
{
    CASpringAnimation * anim = [CASpringAnimation animation];
    anim.keyPath = @"position.y";
    anim.fromValue =@(self.selectImageV.center.y);
    anim.toValue = @(self.selectImageV.center.y - 450);

    anim.mass = 10;
    
    anim.initialVelocity = 2;

    anim.duration = anim.settlingDuration;
    
    [self.selectImageV.layer addAnimation:anim forKey:@"position.y"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        ViewController *vc = [[ViewController alloc]init];
        vc.myCarName = self.leftCar.selected?@"玩家车车 (1)":@"玩家车车 (2)";
        [[KVPlayer shareTool]stop];
        [self presentViewController:vc animated:YES completion:^{
            self.selectImageV.y = 0;
        }];
    });

}
- (IBAction)record:(id)sender {
    recordViewController *vc = [[recordViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
    [nav.navigationBar setBarTintColor:[UIColor blackColor]];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
